package net.hvacapp.cmvrfforandroid.ui.mine;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_user_approve)
public class UserApproveActivity extends BaseActivity implements TabLayout.OnTabSelectedListener{

    @ViewById(R.id.user_approve_tablayout)
    TabLayout mProjectTabL;

    UserListFragment mFragmentApproved;
    UserListFragment mFragmentNotApproved;

    /**
     * 页面加载完后执行
     */
    @AfterViews
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();

        initEvent();
    }

    /**
     * 初始化
     */
    @Override
    public void initBoot() {
        setupToolbar(0, "用户管理", null);
    }

    /**
     * 初始化数据
     */
    @Override
    public void initData(Intent intent) {
        mProjectTabL.addTab(mProjectTabL.newTab().setText("未认证用户"), true);
        mProjectTabL.addTab(mProjectTabL.newTab().setText("已认证用户"), false);
        mProjectTabL.setOnTabSelectedListener(this);

        setTabSelection(0);

    }



    private void initEvent() {

    }

    /**
     * 页面名称
     */
    @Override
    public String getName() {
        return "";
    }


    /**
     * 设置tab页
     * @param position 0:概况  1:关键词  2:排名
     */
    private void setTabSelection(int position) {
        // 开启一个Fragment事务
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);

        switch (position) {
            case 1:
                if (mFragmentApproved == null) {
                    mFragmentApproved = UserListFragment_.builder().arg("typeValue", 1).build();
                    transaction.add(R.id.content_fl, mFragmentApproved);
                } else {
                    transaction.show(mFragmentApproved);
                }
                break;
            case 0:
                if (mFragmentNotApproved == null) {
                    mFragmentNotApproved = UserListFragment_.builder().arg("typeValue", 0).build();
                    transaction.add(R.id.content_fl, mFragmentNotApproved);
                } else {
                    transaction.show(mFragmentNotApproved);
                }
                break;
        }

        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (mFragmentApproved != null) {
            transaction.hide(mFragmentApproved);
        }
        if (mFragmentNotApproved != null) {
            transaction.hide(mFragmentNotApproved);
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        switch (tab.getPosition()) {
            case 0:
                setTabSelection(0);
                break;
            case 1:
                setTabSelection(1);
                break;
            case 2:
                setTabSelection(2);
                break;
            case 3:
                setTabSelection(3);
                break;
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
